﻿<div id="chat-banner"><h3>Spjallið!</h3></div>

@if (Request.IsAuthenticated) {
<div class="container">
    <input type="hidden" id="displayname" />
    <ul id="discussion">
    </ul>
    <input type="text" id="message" />
    <input class="btn btn-danger" type="button" id="sendmessage" value="Send" />
</div>


    <!--Script references. -->
    <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
    <!--Reference the SignalR library. -->
    <script src="~/Scripts/jquery.signalR-1.0.1.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
    <!--SignalR script to update the chat page and send messages.--> 
    <script>
        $(function () {
            // Reference the auto-generated proxy for the hub.  
            var chat = $.connection.chatHub;
            // Create a function that the hub can call back to display messages.
            chat.client.addNewMessageToPage = function (name, message) {
                // Add the message to the page. 
                $('#discussion').append('<li><strong>' + htmlEncode(name)
                    + '</strong>: ' + htmlEncode(message) + '</li>');
            };
            // Get the user name and store it to prepend to messages.
            var user = '@User.Identity.Name';
            $('#displayname').val(user);
            // Set initial focus to message input box.  
            // Start the connection.
            $.connection.hub.start().done(function () {

                window.addEventListener('keypress', function (e) {
                    //adferd til ad leyfa notenda ad yta a enter til ad senda spjall
                    if (e.keyCode === 13) {
                        if ($('#message').val() != "") {
                            //console.log("AAA");
                            // Call the Send method on the hub. 
                            chat.server.send($('#displayname').val(), $('#message').val());
                            // Clear text box and reset focus for next comment. 
                            $('#message').val('').focus();
                        }
                    }
                }, false);

                $('#sendmessage').click(function () {
                    // Call the Send method on the hub. 
                    if ($('#message').val() != "") {
                        chat.server.send($('#displayname').val(), $('#message').val());
                        // Clear text box and reset focus for next comment. 
                        $('#message').val('').focus();
                    }
                });
            });
        });
        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>
}
else
{
     <ul class="loginToChat">
         <div>
        <li>Skráðu þig @Html.ActionLink(" inn", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" }) til að nota spjallið</li>
         <li> Áttu ekki aðgang? @Html.ActionLink("Nýskráðu þig", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
         </div>
    </ul>
}